﻿
/**
* datagrid 数据绑定
**/
function dataGridBind(dg, url, title, toolbar, number,size) {
    if (!number)
        number = 1;
    else
        number = parseInt(number);

    if (!size)
        size = 10;
    else
        size = parseInt(size);

    dg.datagrid({
        url: url,
        method: 'get',
        title: title,
        iconCls: 'icon-edit',
        toolbar: toolbar,
        width: 'auto',
        height: 'auto',
        nowrap: false,
        striped: true, //条纹
        border: true,
        collapsible: false,//是否可折叠的 
        fit: false,//自动大小 
        singleSelect: true,//是否单选 
        rownumbers: true,//行号 
        pagination: true,
        pageSize: size,
        pageNumber: number,
        onClickRow: onClickRow,
        loader: myLoader,
        onHeaderContextMenu: function (e, field) {
            e.preventDefault();
            if (!cmenu) {
                createColumnMenu();
            }
            cmenu.menu('show', {
                left: e.pageX,
                top: e.pageY
            });
        }
    });
}

var cmenu;
function createColumnMenu() {
    cmenu = $('<div/>').appendTo('body');
    cmenu.menu({
        onClick: function (item) {
            if (item.iconCls == 'icon-ok') {
                $('#dg').datagrid('hideColumn', item.name);
                cmenu.menu('setIcon', {
                    target: item.target,
                    iconCls: 'icon-empty'
                });
            } else {
                $('#dg').datagrid('showColumn', item.name);
                cmenu.menu('setIcon', {
                    target: item.target,
                    iconCls: 'icon-ok'
                });
            }
        }
    });
    var fields = $('#dg').datagrid('getColumnFields');
    for (var i = 0; i < fields.length; i++) {
        var field = fields[i];
        var col = $('#dg').datagrid('getColumnOption', field);
        cmenu.menu('appendItem', {
            text: col.title,
            name: field,
            iconCls: 'icon-ok'
        });
    }
}

/**
* datagrid 强制刷新
**/
function myLoader(param, success, error) {
    var that = $(this);
    var opts = that.datagrid("options");
    if (!opts.url) {
        return false;
    }

    var ajaxCached = !(that.attr("ajaxCached") === "false");//不使用ajax缓存

    $.ajax({
        type: opts.method,
        url: opts.url,
        cache: ajaxCached,
        data: param,
        dataType: "json",
        success: function (data) {
            success(data);
        },
        error: function () {
            error.apply(this, arguments);
        }
    });
}

/**
* datagrid 单击选中
**/
function onClickRow(index) {
    var grid = $('#dg');
    var selIndex = grid.data().datagrid['selectRowIndex'];

    if (selIndex != index) {
        grid.datagrid('selectRow', index);
        selIndex = index;
    }
    else {
        grid.datagrid('selectRow', -1);
        selIndex = null;
    }

    grid.data().datagrid['selectRowIndex'] = selIndex;
}

/**公里桩号格式化**/
function formatPile(val, row) {
    if (val == 0) return "K0";
    if (!val) return;

    var k = Math.floor(val / 1000);
    var r = (val % 1000).toFixed(2);
    r = r + "";
    r = r.replace(".00", "");
    return "K" + k + "+" + r;
}

/**时间格式化**/
function formatDate(date, row) {
    if (!date) return;
    var index = date.indexOf('T');
    if (index > 0)
        return date.substring(0, index);
    else
        return date;
}
/**日期格式化**/
function formatterDate(date) {
    var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
    var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
    + (date.getMonth() + 1);
    return date.getFullYear() + '-' + month + '-' + day;
};
/**交通等级格式化**/
function formatTrafficrank(val,row) {
    if (!val) return;
    if (val == 4001) return "特重";
    if (val == 4002) return "重";
    if (val == 4003) return "中等";
    if (val == 4004) return "轻";
}


/**数字格式化，2个小数**/
function formatNumber(value, row, index) {
   
    var number = Number(value);
    var fixed = 2;
   
    //彭灿明修改： 如果VALUE的值为NULL，返回'';
    if (value == null)
    {
        return '';
    }
    else if (isNaN(number)) {
        return '';
    } else {
        if (fixed == 2) {
            //彭灿明修改 第二位小数为0不显示问题
            //return number.toFixed(2) * 100 / 100;
            return number.toFixed(2);
        } else if (fixed == 6) {
            return number.toFixed(6) * 1000000 / 1000000;
        }
    }
}

function formatBold(val, row) {
    return '<span style="font-weight:bold;">' + val + '</span>';
}

/**
        * EasyUI DataGrid根据字段动态合并单元格
        * @param fldList 要合并table的id
        * @param fldList 要合并的列,用逗号分隔(例如："name,department,office");
        */
function MergeCells(tableID, fldList) {
    var Arr = fldList.split(",");
    var dg = $('#' + tableID);
    var fldName;
    var RowCount = dg.datagrid("getRows").length;
    var span;
    var PerValue = "";
    var CurValue = "";
    var length = Arr.length - 1;
    for (i = length; i >= 0; i--) {
        fldName = Arr[i];
        PerValue = "";
        span = 1;
        for (row = 0; row <= RowCount; row++) {
            if (row == RowCount) {
                CurValue = "";
            }
            else {
                CurValue = dg.datagrid("getRows")[row][fldName];
            }
            if (PerValue == CurValue) {
                span += 1;
            }
            else {
                var index = row - span;
                dg.datagrid('mergeCells', {
                    index: index,
                    field: fldName,
                    rowspan: span,
                    colspan: null
                });
                span = 1;
                PerValue = CurValue;
            }
        }
    }
}

/**
        * EasyUI DataGrid根据字段动态合并单元格,第一列是复选框
        * @param fldList 要合并table的id
        * @param fldList 要合并的列,用逗号分隔(例如："name,department,office");
        */

function MergeCellsInChecks(tableID,fldCheck, fldList) {
    var Arr = fldList.split(",");
    var dg = $('#' + tableID);
    var fldName;
    var RowCount = dg.datagrid("getRows").length;
    var span;
    var PerValue = "";
    var CurValue = "";
    var length = Arr.length - 1;
    for (i = length; i >= 0; i--) {
        fldName = Arr[i];
        PerValue = "";
        span = 1;
        for (row = 0; row <= RowCount; row++) {
            if (row == RowCount) {
                CurValue = "";
            }
            else {
                CurValue = dg.datagrid("getRows")[row][fldName];
            }
            if (PerValue == CurValue) {
                span += 1;
            }
            else {
                var index = row - span;
                dg.datagrid('mergeCells', {
                    index: index,
                    field: fldName,
                    rowspan: span,
                    colspan: null
                });

                ///将复选框也一起合并
                if (i == 0)
                {
                    dg.datagrid('mergeCells', {
                        index: index,
                        field: fldCheck,
                        rowspan: span,
                        colspan: null
                    });
                }

                span = 1;
                PerValue = CurValue;
            }
        }
    }
}